<template>
  <div class="login">
    <div class="login-conent">
      <div class="form">
          <div class="logo"><img src="@/assets/images/icon.svg" alt=""></div>
           <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
    <!-- prop 是为了做规则校验 -->
      <el-form-item label="用户名" prop="username">
        <el-input type="text" v-model.number="ruleForm.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="pass">
        <el-input
          type="password"
          v-model="ruleForm.pass"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPass">
        <el-input
          type="password"
          v-model="ruleForm.checkPass"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >注册</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
        <el-button type="success" @click="handleClick">登录</el-button>
      </el-form-item>
    </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
    // beforeRouteEnter () {
    //     // 在局部守卫中获取不到this
    //     console.log(1);
    // },
  data() {
    var checkUsername = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("用户名不能为空"));
      }else{
          callback();
      }
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        pass: "",
        checkPass: "",
        username: "",
      },
      rules: {
        pass: [{ validator: validatePass, trigger: "blur" }],
        checkPass: [{ validator: validatePass2, trigger: "blur" }],
        username: [{ validator: checkUsername, trigger: "blur" }],
      },
    };
  },
  methods: {
    handleClick(){
      this.$router.push("/login")
    },
    submitForm(formName) {
        
      this.$refs[formName].validate((valid) => {
        if (valid) {
        //   alert("submit!");
          this.loginHttp();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    loginHttp(){
      console.log(this.ruleForm);
      var {username,pass} = this.ruleForm;
      this.$https({
        url:"http://127.0.0.1:7001/register",
        method:"post",
        data:{
          username,
          pass
        }
      }).then(res=>{
        console.log(res.data)
        if(res.data.code == 200){
            this.$message({
                message:res.data.msg,
                type:"success"
            })
            this.$router.push("/login")
        }else{
            this.$message({
                message:res.data.msg,
                type:"warning"
            })
        }
      })
    }
  },
};
</script>

<style scoped>
.login {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-image: linear-gradient(to right, #a5fecb, #20bdff );
}
.login-conent{
   width: 100%;
   height: 100%;
   position: relative;
}
.form {
text-align: center;
  position: absolute;
  width: 500px;
  height: 300px;
  /* background-color: pink; */
  left: 50%;top: 50%;
  transform: translate(-50%,-50%);
}
.logo {
    margin-left: 100px;
    margin-bottom: 30px;
}
</style>